<template>
  <el-dialog
    :title="spec.spec.id ? '修改' : '新增'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    {{spec}}
    <el-form :model="spec" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="specName">
      <el-input v-model="spec.spec.specName" placeholder="名称"></el-input>
    </el-form-item>
      <el-button type="success" plain @click="addRow">新增规格选项</el-button>
      <el-table
        :data="spec.options"
        style="width: 100%">
        <el-table-column
          label="规格选项"
          align="center"
          width="280px">
          <template slot-scope="scope" >
            <el-input v-model="scope.row.optionName" placeholder="规格选项"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          label="排序"
          align="center"
          width="280px">
          <template slot-scope="scope">
            <el-input v-model="scope.row.orders" placeholder="排序"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          label="排序"
          align="center"
          >
          <template slot-scope="scope">
            <el-button  type="danger" @click="spec.options.splice(scope.$index,1)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        spec:{
          spec:{
            id: 0,
            specName: '',
          },
          options:[]
        },
        dataRule: {
          spec:{
            specName: [
              { required: true, message: '名称不能为空', trigger: 'blur' }
            ]
          }

        }
      }
    },
    methods: {
      addRow(){
        this.spec.options.push({});
      },
      init (id) {
        this.spec.spec.id = id;
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.spec.spec.id) {   //有id就从后台查询此规格及其规格选项列表
            this.$http({
              url: this.$http.adornUrl(`/manager/specification/info/${this.spec.spec.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                console.log("data---->",data);
                //this.spec.spec.specName = data.specification.specName
                this.spec = data.specification;
              }
            })
          }else{  //如果是添加就设置默认值
            this.spec={spec:{specName: '',id:0}};
            this.spec.options=[];
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manager/specification/${!this.spec.spec.id ? 'save' : 'update'}`),
              method: `${this.spec.spec.id ? 'put' : 'post'}`,
              // data: this.$http.adornData({
              //   'id': this.spec.spec.id || undefined,
              //   'specName': this.spec.spec.specName
              // })
              data: this.$http.adornData(this.spec)
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
